package top.lyf1210.yuwenrpcspringbootstarter.bootstrap;

import com.yuwen.RpcApplication;
import com.yuwen.yuwenRPC.config.RpcConfig;
import com.yuwen.yuwenRPC.server.tcp.VertxTcpServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
import top.lyf1210.yuwenrpcspringbootstarter.annotation.EnableRpc;

/**
 * @ClassNameRpcInitBootstrap
 * @Description TODO
 * @Author钰玟
 * @Date2025/4/6 下午3:17
 * @Version 1.0
 **/
@Slf4j
public class RpcInitBootstrap implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        /**
         * 获取enableRpc注解的属性值
         */
        boolean needServer = (boolean) importingClassMetadata.getAnnotationAttributes(EnableRpc.class.getName())
                .get("needServer");

        // RPC 框架初始化（配置和注册中心）
        RpcApplication.init();

        // 全局配置
        final RpcConfig rpcConfig = RpcApplication.getRpcConfig();

        // 启动服务器
        if (needServer) {
            VertxTcpServer vertxTcpServer = new VertxTcpServer();
            vertxTcpServer.doStart(rpcConfig.getPort());
        } else {
            log.info("不启动 server");
        }
    }
}
